﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using hzGis.model;
using hzGis.utils;
using System.IO;
using hzGis.service.impl;
using hzGis.config;

namespace hzGis.Component
{
    public partial class SysConfigWin : Form
    {
        private FileService fileService;
        private YdbpWin ydbpWin;
        public SysConfigWin(YdbpWin t_ydbpWin)
        {
            InitializeComponent();
            fileService = new FileService();
            init();
            ydbpWin = t_ydbpWin;
        }

        private void xqShpBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择县区shp";
            dialog.Filter = "shp|*.shp";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                xqShp.Text = dialog.FileNames[0];
            }
        }

        private void zjLyrBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择镇界lyr";
            dialog.Filter = "lyr|*.lyr";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                zjLyr.Text = dialog.FileNames[0];
            }
        }

        private void cjLyrBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择村界lyr";
            dialog.Filter = "lyr|*.lyr";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                cjLyr.Text = dialog.FileNames[0];
            }
        }

        private void xztlBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择现状图例png";
            dialog.Filter = "png|*.png";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                xzTl.Text = dialog.FileNames[0];
            }
        }

        private void fxTlBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择分析图例png";
            dialog.Filter = "png|*.png";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                fxTl.Text = dialog.FileNames[0];
            }
        }

        private void dlBtn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "请选择地类所在的文件夹";
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                dl.Text = folderBrowser.SelectedPath;
            }
        }

        private void tfhBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择图幅号shp";
            dialog.Filter = "shp|*.shp";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tfh.Text = dialog.FileNames[0];
            }
        }

        private void resultBtn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "请选择结果存放的文件夹";
            //folderBrowser.ShowNewFolderButton = true;
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                resultLj.Text = folderBrowser.SelectedPath;
            }
        }

        private void sure_Click(object sender, EventArgs e)
        {
            string res = validate();
            if (res != "")
            {
                MessageBox.Show(res);
            }
            else
            {
                string dirPath = fileService.getCurrentPath();
                ToolConfig toolConfig = new ToolConfig();
                toolConfig.xqShp = this.xqShp.Text;
                toolConfig.zjLyr = this.zjLyr.Text;
                toolConfig.cjLyr = this.cjLyr.Text;
                toolConfig.xztl = this.xzTl.Text;
                toolConfig.fxTl = this.fxTl.Text;
                toolConfig.dl = this.dl.Text;
                toolConfig.tfh = this.tfh.Text;
                toolConfig.resultLj = this.resultLj.Text;
                toolConfig.scale = this.scale.Text;
                toolConfig.dbUrl = this.dbUrl.Text;
                toolConfig.dbUser = this.dbUser.Text;
                toolConfig.dbName = this.dbName.Text;
                toolConfig.dbPwd = this.dbPwd.Text;
                toolConfig.dbPort = this.dbPort.Text;
                toolConfig.dlJsonFile = this.dlJsonFile.Text;
                string configStr = JsonHelper.JsonSerializer<ToolConfig>(toolConfig);
                string configPath = dirPath + "\\" + SysConfig.CONFIGNAME;
                if (File.Exists(configPath))
                {
                    File.Delete(configPath);
                }
                File.WriteAllText(configPath, configStr);
                ydbpWin.intWin(toolConfig);
                this.Close();

            }
        }
        private string validate()
        {
            string res = "";
            if (this.xqShp.Text == null || this.xqShp.Text == ""){res += "请选择县区shpfile文件\n\r";}
            if (this.zjLyr.Text == null || this.zjLyr.Text == "") { res += "请选择镇界lyr文件\n\r"; }
            if (this.cjLyr.Text == null || this.cjLyr.Text == "") { res += "请选择村界lyr文件\n\r"; }
            if (this.xzTl.Text == null || this.xzTl.Text == "") { res += "请选择现状图例图片\n\r"; }
            if (this.fxTl.Text == null || this.fxTl.Text == "") { res += "请选择分析图例图片\n\r"; }
            if (this.dl.Text == null || this.dl.Text == "") { res += "请选择地类所在的文件夹\n\r"; }
            if (this.tfh.Text == null || this.tfh.Text == "") { res += "请选择图幅号shpfile文件\n\r"; }
            if (this.resultLj.Text == null || this.resultLj.Text == "") { res += "请选择结果存放的文件夹\n\r"; }
            if (this.dbName.Text == null || this.dbName.Text == "") { res += "请输入数据库名称\n\r"; }
            if (this.dbUrl.Text == null || this.dbUrl.Text == "") { res += "请输入数据库链接\n\r"; }
            if (this.dbUser.Text == null || this.dbUser.Text == "") { res += "请输入数据库用户名称\n\r"; }
            if (this.dbPwd.Text == null || this.dbPwd.Text == "") { res += "请输入数据库用户密码\n\r"; }
            if (this.dbPort.Text == null || this.dbPort.Text == "") { res += "请输入数据库链接端口\n\r"; }
            if (this.dlJsonFile.Text == null || this.dlJsonFile.Text == "") { res += "请输入建设用地地类json文件路径\n\r"; }
            bool dbCheck = DBHelper.checkConnection(this.dbUrl.Text, this.dbUser.Text, this.dbPwd.Text, this.dbName.Text, this.dbPort.Text);
            if (!dbCheck)
            {
                res += "请输入数据库链接不上，请检查配置\n\r";
            }
            return res;
        }

        

        private void init()
        {
            string dirPath = fileService.getCurrentPath();
            string configPath = dirPath + "\\" + SysConfig.CONFIGNAME;
            if (File.Exists(configPath))
            {
                ToolConfig toolConfig = JsonHelper.JsonDeserialize<ToolConfig>(File.ReadAllText(configPath));
                this.xqShp.Text = toolConfig.xqShp;
                this.zjLyr.Text = toolConfig.zjLyr;
                this.cjLyr.Text = toolConfig.cjLyr;
                this.xzTl.Text = toolConfig.xztl;
                this.fxTl.Text = toolConfig.fxTl;
                this.dl.Text = toolConfig.dl;
                this.tfh.Text = toolConfig.tfh;
                this.resultLj.Text = toolConfig.resultLj;
                this.scale.Text = toolConfig.scale;
                this.dbUrl.Text = toolConfig.dbUrl;
                this.dbUser.Text = toolConfig.dbUser;
                this.dbPwd.Text = toolConfig.dbPwd;
                this.dbName.Text = toolConfig.dbName;
                this.dbPort.Text = toolConfig.dbPort;
                this.dlJsonFile.Text = toolConfig.dlJsonFile;
            }
        }

        private void dlJsonFileBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "请选择建设用地地类json文件";
            dialog.Filter = "json|*.json";
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.dlJsonFile.Text = dialog.FileNames[0];
            }
        }
    }
}
